//
//  ViewController.m
//  UI01_UIVieww
//
//  Created by dllo on 16/6/3.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ViewController.h"
#import "CustomView.h"
@interface ViewController ()

@property (nonatomic,retain)CustomView *rainView;




@end

@implementation ViewController
-(void)dealloc{
    [_rainView release];
    
    [super dealloc];
}




-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil  bundle:nibBundleOrNil];
    if(self){
        NSLog(@"%d,%s",__LINE__, __FUNCTION__);
    }
    return self;
}
-(instancetype)init{
    self = [super init];
    if(self){
NSLog(@"%d,%s",__LINE__, __FUNCTION__);
    }
    return self;
}
//加载视图
-(void)loadView{
    [super loadView];
    NSLog(@"%d,%s",__LINE__, __FUNCTION__);
}
//加载完成视图  加载子视图.
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSLog(@"%d,%s",__LINE__, __FUNCTION__);
    self.view.backgroundColor = [UIColor greenColor];
//    [self creadSubViews];

    //    [self addCustomViews];
    [self addCustomViews];
    [self creatButtons];
}
//视图将要显示
-(void)viewDidAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    NSLog(@"%d,%s",__LINE__ , __FUNCTION__);
}
//视图显示完成
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillDisappear:animated];
    NSLog(@"%d,%s",__LINE__, __FUNCTION__);
}
//创建自定义方法,写加载子视图的代码.

-(void)creadSubViews{
    UIView *blueView = [[UIView alloc]initWithFrame:CGRectMake(10, 50, 200, 300)];
//    视图背景颜色默认是ClearColor.
    blueView .backgroundColor = [UIColor blueColor];
//    将子视图添加到父视图上.   self.view是blueView的父视图
    [self.view addSubview:blueView];
//    内存管理
    [blueView release];
//    练习:创建一个视图,距离屏幕左边50距离屏幕上边400,宽50 高30,背景颜色为黄色.
    
    
    UIView *yellowView = [[UIView alloc]initWithFrame:CGRectMake(50, 400, 50, 30)];
    yellowView.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:yellowView];
    [yellowView release];
    
    UIView *blackView = [[UIView alloc]initWithFrame:CGRectMake(10, 20, 30,50)];
    blackView.backgroundColor = [UIColor blackColor];
    [blueView addSubview:blackView];
    [blackView release];
//    frame是父视图为参照的坐标系统.
    
//    UIView常用API.
//    隐藏视图(包括子数图)默认为NO;为YES将视图隐藏.
    blueView.hidden = NO;
//    改变视图的透明度.CGFloat类型,范围从0.0到1.0,默认值为1.0;   1.0是完全不透明的状态
    blueView.alpha = 0.5;
    
//    获取视图的子视图
    NSLog(@"blueView subViews:%@",blueView.subviews);
//    获取父视图(父视图只有唯一的一个).
    NSLog(@"blueView superView:%@",blueView.superview);
//    视图中心点(对角线交叉的地方);
    blueView.center = CGPointMake(100, 200);
    NSLog(@"x:%lf,y:%lf,w:%lf,h:%lf",blueView.frame.origin.x,blueView.frame.origin.y,blueView.frame.size.width,blueView.frame.size.height);
//    视图的标记.tag;
    blueView.tag = 100;
    
    UIView *findView = [self.view viewWithTag:100];
    NSLog(@"bluevie:%@",blueView);
    NSLog(@"findview:%@",findView);
    
//    输出一下self.view的子视图.
    NSArray *svSubViewsArray = self.view.subviews;
    NSLog(@"self.view的子视图%@",svSubViewsArray);
    
//    创建一个视图.坐标为(50,100),宽高为(200,300),颜色为橙色.添加到self.view上.
    UIView *orangeView = [[UIView alloc]initWithFrame:CGRectMake(50, 100, 200, 300)];
    orangeView.backgroundColor = [UIColor orangeColor ];
[self.view addSubview:orangeView];
    [orangeView release];

    UIView *purpleView = [[UIView alloc]initWithFrame:CGRectMake(100, 150, 200, 300)];
    purpleView.backgroundColor = [UIColor purpleColor ];
    [self.view addSubview:purpleView];
    [purpleView release];
    
    
//    把blueView放在所有子视图的前面 .
    [self.view bringSubviewToFront:blueView];
//    把purpleview放在所有子视图的后面.
    [self.view sendSubviewToBack:purpleView];
//    创建视图.深灰色视图.frame()
    UIView *redView = [[UIView alloc]initWithFrame:CGRectMake(110, 220, 50, 30)];
    redView.backgroundColor = [UIColor redColor];
//    插入下标为二的视图
    [self.view insertSubview:redView  atIndex:2];
    
    [redView release];
//    插入到在指定视图的前面
    UIView *brownView = [[UIView alloc]initWithFrame:CGRectMake(20, 203, 222, 111)];
    brownView.backgroundColor = [UIColor brownColor];
    [self.view insertSubview:brownView aboveSubview:orangeView];
    [brownView release];
//    插入到指定视图的后面
    UIView *cyanView = [[UIView alloc]initWithFrame:CGRectMake(20, 203, 222, 111)];
   cyanView.backgroundColor = [UIColor cyanColor];
    [self.view insertSubview:cyanView belowSubview:orangeView];
    [cyanView release];
    
//    交换指定下标位置上的视图
    [self.view exchangeSubviewAtIndex:4 withSubviewAtIndex:0];
//    将子视图从父视图上移除.
    [cyanView removeFromSuperview];
    
    
    
    }



// 添加自定义的视图代码.
-(void)addCustomViews{
    CustomView *customView  = [[CustomView alloc]initWithFrame:CGRectMake(50, 100, 100, 200)];
   customView.tag = 100;
    
    
    [self.view addSubview:customView];
    [customView release];
//    创建属性
    self.rainView = [[CustomView alloc]initWithFrame:CGRectMake(200, 300, 100, 200) withColor:[UIColor blackColor]];
    [self.view addSubview: _rainView];
    [_rainView release];
    
    
    
    
    
    
}
//CustomView *secondView =[ [CustomView alloc]initWithFrame:CGRectMake(50, 100, 110, 50) withColor:[UIColor greenColor]];
//[self.view addSubview:CustomView];

#pragma mark -UIButton
-(void)creatButtons{
    
    
//创建Button
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
//    设置背景颜色
    button.backgroundColor = [UIColor orangeColor];
//    确定在父视图的位置
    button.frame = CGRectMake(100, 400, 50, 40);
//    添加到父视图上
    [self.view addSubview:button];
//    不需要对button进行release;
//    添加点击事件.
    [button addTarget:self action:@selector(changeColorAction:) forControlEvents:UIControlEventTouchUpInside];
    
}

//parameterButton 就是 button
-(void)changeColorAction:(UIButton*)prameterButton{
//    改变self.view的背景颜色.
    self.view.backgroundColor = [UIColor grayColor];
    
    CustomView *findView = (CustomView*) [self.view viewWithTag:100];
//    通过tag值找到指定的视图.
    findView.backgroundColor = [UIColor orangeColor];
//    通过属性改变指定视图的背景颜色.
    self.rainView.backgroundColor = [UIColor whiteColor];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
